package com.gitee.feizns.explore.data_structure.array_and_string.rotate;

/**
 * @author feizns
 * @since 2019/10/25
 */
public class Rotate2 {

    static int[][] Arr = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };

    public static void main(String[] args) {
        print();

        System.out.println();

        rotate(Arr);

        System.out.println();

        print();
    }

    public static void rotate(int[][] matrix) {

        //转置+翻转
        int n = matrix.length;

        //转置
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                int tmp = matrix[j][i];
                matrix[j][i] = matrix[i][j];
                matrix[i][j] = tmp;
            }
        }

        print();

        //翻转
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n / 2; j++) {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[i][n - j - 1];
                matrix[i][n - j - 1] = tmp;
            }
        }

    }

    private static void print() {
        for (int i = 0; i < Arr.length; i++) {
            for (int j = 0; j < Arr.length; j++) {
                System.out.print(Arr[i][j] + "\t");
            }
            System.out.println();
        }
    }

}
